import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.TimeUnit;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.google.common.base.Function;

public class SeleniumBase {

	private static WebDriver driver;
	protected static String baseUrl;

	@BeforeClass
	public static void setUp() throws Exception {
		driver = new FirefoxDriver();
		baseUrl = "http://blog.testowka.pl/";
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}

	@AfterClass
	public static void tearDown() throws Exception {
		driver.quit();

	}

	protected void assertTextPresent(String text) {
		assertTrue(driver.getPageSource().contains(text));
	}

	private void assert_tytul(String tytul) {
		assertEquals(tytul, driver.getTitle());
	}

	protected void insertText(By ID, String Text) {
		WebElement findElement = driver.findElement(ID);
		findElement.sendKeys(Text);
	}

	protected void waitForElement(final By identifier,final String Text) {
		WebDriverWait wait=new WebDriverWait(driver,10);
		
		wait.until(new Function<WebDriver, Boolean>() {
			@Override
			public Boolean apply(WebDriver d) {
				return d.findElement(identifier).getText().contains(Text);
			};
		});
	}

	protected void click(By ID) {
		driver.findElement(ID).click();

	}

	protected void open_www(String www) {
		driver.get(www);
	}

	public SeleniumBase() {
		super();
	}

	protected boolean isElementPresent(By ID) {
		try {
			driver.findElement(ID);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}

}